<?php
namespace core\models;
/**
 * 前台Mysql数据库类
 * @package 数据库
 * @author ZhouHaoHua <53027327@qq.com> Administrator
 */
class Database_Mysql extends Abstract_Database{

    protected function connect($server, $username, $password, $database){
		if(!$this->link){
			if (USE_PCONNECT == 'true') {
				$this->link = mysql_pconnect($server, $username, $password, MYSQL_CLIENT_COMPRESS) or die('Unable to connect to database server!');				//开启持久连接(用多数据库时请勿开启)
				//echo '持久连接';
			} else {
				$this->link = mysql_connect($server, $username, $password, true, MYSQL_CLIENT_COMPRESS) or die('Unable to connect to database server!');		//不开启持久连接
				//echo '普通连接';
			}			
			if ($this->link) mysql_select_db($database, $this->link) or die('Unable to open to database '.$database.'!');		//如果连接上库则选择具体的数据库
//			$this->charset && mysql_query('set names ' . $this->charset, $this->link);
			//echo '1次;';
			mysql_query('set names "'.$this->charset.'"',$this->link);
		}
		return $this->link;
	}

	final public function close(){
        return $this->link ? mysql_close($this->link) : 0;
	}

	final public function query($query){
		// STORE_DB_TRANSACTIONS : Store the database queries in the page parse time log
		// 如果STORE_DB_TRANSACTIONS定义了并且值为true, 那么将$query保存到日志
		// error_log(error,type,destination,headers) 函数向服务器错误记录、文件或远程目标发送一个错误。
		// error 必需,要记录的错误消息; type 可选,规定错误记录的类型；destination 可选,规定向何处发送错误消息,该参数的值依赖于 "type" 参数的值; headers 可选,只在 "type" 为 1 时使用。
		// STORE_PAGE_PARSE_TIME_LOG 数据库 configuration 表中定义
		if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
			error_log('QUERY ' . $query . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
		}

		$result = mysql_query($query, $this->link) or $this->error($query, mysql_errno(), mysql_error());
		
		if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
			$result_error = mysql_error();
			error_log('RESULT ' . $result . ' ' . $result_error . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
		}
		
		return $result;
	}

	final public function fetch_array($db_query){
		return mysql_fetch_array($db_query, MYSQL_ASSOC);
	}

	final public function num_rows($db_query){
		return mysql_num_rows($db_query);
	}

	final public function data_seek($db_query, $row_number){
		return mysql_data_seek($db_query, $row_number);
	}

	final public function insert_id(){
		return mysql_insert_id($this->link);
	}

	final public function free_result($db_query) {
		return mysql_free_result($db_query);
	}

	final public function fetch_fields($db_query) {
		return mysql_fetch_field($db_query);
	}

	final public function input($string) {
		if (function_exists('mysql_real_escape_string')) {
			return mysql_real_escape_string($string, $this->link);
		} elseif (function_exists('mysql_escape_string')) {
			return mysql_escape_string($string);
		}	
		if(!get_magic_quotes_gpc()){
			return addslashes($string);
		}
		return $string;
	}

	final public function affected_rows(){
		return mysql_affected_rows($this->link);
	}

	public function __destruct(){
		return $this->close();
	}

    public function table_exists($table_name){
        $result = mysql_fetch_row(mysql_query("SHOW TABLES LIKE '{$table_name}' "));
        if($result && is_array($result)){
            return true;
        }else{
            return false;
        }
    }

}
